//
//  ChooseLocationViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/6/21.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

//@property (weak, nonatomic) IBOutlet MAMapView *mapView;
//

//// 自定义大头针
//@property (nonatomic, strong) UIImageView          *centerAnnotationView;

import UIKit

protocol ChooseLocationDelegate:NSObjectProtocol{
    func postValueToVc(address:String,lon:String,lat:String)
}

class ChooseLocationViewController: BaseViewController {
    
    weak var chooseLoction:ChooseLocationDelegate?
    
    @IBOutlet var mapView: MAMapView!
    
    var centerAnnotationView :UIImageView?
    
    var address = ""
    var lat = ""
    var lon = ""
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "选择地址"
        initMapView()
        baseSetNavRightButtonWithTitle("确定")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        initCenterView()
    }
    
    override func baseNavRightButtonPressed(_ button: UIButton) {
        if self.address == "" || self.lon == "" || self.lat == "" {
            return
        }
        chooseLoction?.postValueToVc(address: self.address, lon: self.lon, lat: self.lat)
    self.navigationController?.popViewController(animated: true)
    }
    
    func initMapView() -> Void {
        mapView.showsUserLocation = true
        mapView.showsCompass = false
        mapView.showsScale = false
        mapView.userTrackingMode = MAUserTrackingMode.follow
        mapView.delegate = self
        mapView.zoomLevel = 17
    }
    
    func initCenterView() -> Void {
 
        centerAnnotationView = UIImageView.init(image: UIImage.init(named: "homePage_wholeAnchor"))
        centerAnnotationView?.center = CGPoint(x: mapView.center.x, y: mapView.center.y)
        mapView.addSubview(centerAnnotationView!)
    }
    
    ///将经纬度转换为城市名
    func LonLatToCity(currLocation:CLLocation) {
        let geocoder: CLGeocoder = CLGeocoder()
        geocoder.reverseGeocodeLocation(currLocation) { (placemark, error) -> Void in
            
            if(error == nil)
            {
                let array = placemark! as NSArray
                let mark = array.firstObject as! CLPlacemark
                //                //街道位置
                let FormattedAddressLines: NSString = ((mark.addressDictionary! as NSDictionary).value(forKey: "FormattedAddressLines") as AnyObject).firstObject as! NSString
                //                //具体位置
                let Name: NSString = (mark.addressDictionary! as NSDictionary).value(forKey: "Name") as! NSString
                print(FormattedAddressLines)
                print(Name)
                self.address = Name as String
            }
            else
            {
                print(error)
            }
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension ChooseLocationViewController:MAMapViewDelegate {
    
    func mapView(_ mapView: MAMapView!, regionDidChangeAnimated animated: Bool) {
        
        self.lon = "\(self.mapView.centerCoordinate.longitude)"
        self.lat = "\(self.mapView.centerCoordinate.latitude)"
        
        let currLocation = CLLocation.init(latitude:self.mapView.centerCoordinate.latitude, longitude:self.mapView.centerCoordinate.longitude)
        LonLatToCity(currLocation:currLocation)
        
    }
}

